package engine.control.loading
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import newx.NxSprite;
	
	public class NxLoading extends NxSprite
	{
		private var bgUI:Sprite;
		private var containerUI:Sprite;
		private var barUI:Sprite;
		private var pointUI:Sprite;
		
		private var fullWidth	:uint;
		
		private var _bar		:Number	=	0;
		
		public function NxLoading(_w:uint,_h:uint,_skin:NxLoadingSkin=null)
		{
			if(_skin!=null)
			{
				addChild(_skin);
				barUI	=	_skin.loadingBar;
				bgUI	=	_skin.loadingBG;
				pointUI	=	_skin.loadingPoint;
				fullWidth	=	barUI.width;
				barUI.width	=	0;
			}
			else
			{
				drawLoading(_w,_h);
			}
		}
		
	    public function set skin(_s:MovieClip):void
		{
			if(_s!=null)
			{
				clearDraw()
				addChild(_s);
				barUI	=	_s.loadingBar;
				bgUI	=	_s.loadingBG;
				pointUI	=	_s.loadingPoint;
				fullWidth	=	barUI.width;
				//bar	=	0;
				barUI.width	=	0;
			}
		}
		
		public override function set width(val:Number):void
		{
			this.width
			if(bgUI!=null)
			{
				bgUI.width	=	val;
				barUI.width	=	val-barUI.x*2;
				fullWidth	=	barUI.width;
				bar			=	_bar;
			}
			else
			{
				drawLoading(val,this.height);
			}
		}
		
		private function clearDraw():void
		{
			while(numChildren>0)
			{
				removeChildAt(0);
			}
		}
		
		private function drawLoading(_w:uint,_h:uint):void
		{
			//画背景
			bgUI	=	new Sprite();
			bgUI.graphics.beginFill(0x99ee22);
			bgUI.graphics.drawRect(0,0,_w,_h);
			bgUI.graphics.endFill();
			
			addChild(bgUI);
			
			//画容器
			containerUI	=	new Sprite();
			containerUI.graphics.lineStyle(2,0x55eeff);
			containerUI.graphics.drawRect(0,0,_w,_h);
			addChild(containerUI);
			
			//画进度条
			barUI	=	new Sprite();
			barUI.graphics.beginFill(0xeeee33);
			barUI.graphics.drawRect(0,0,1,_h-4);
			barUI.graphics.endFill();
			addChild(barUI);
			barUI.x	=	barUI.y	=	2;
			
			fullWidth	=	_w	-	4;
		}
		
		public function set bar(val:int):void
		{
			val	=	val<0?0:val;
			val	=	val>100?100:val;
			barUI.width	=	fullWidth*val/100;
			//TweenLite.to(barUI,.5,{width:fullWidth*val/100});//barUI.width	=	
			if(pointUI!=null)
				pointUI.x = barUI.width-30;
			
			_bar	=	val;
		}
		
		public function get bar():int
		{
			return _bar;
		}
		
		public function set progress(val:Number):void
		{
			bar = val * 100;
		}
	}
}